from backstage.data import DataType
import json
import backstage.db as DB


def getDataByDataTypeAndContentlike(**args):
    page = int(args.get("page"))
    limit = int(args.get("limit"))
    start = (page - 1) * limit
    sql = "select id,content,url,sourceType from sys_data_source where 1=1 and sourceType='{sourceType}' and content like '%{content}%' limit {start},{limit}".format(
        sourceType=args.get("sourceType"), content=args.get("content"),
        start=start, limit=limit
    )
    sqlCount = "select count(id) as num from sys_data_source where 1=1 and sourceType='{sourceType}' and content like '%{content}%'".format(
        sourceType=args.get("sourceType"), content=args.get("content")
    )
    count=DB.controlDB(sqlCount).fetchone()
    cursor = DB.controlDB(sql)
    list = []
    if cursor is not None:
        records = cursor.fetchall()
        for item in records:
            d = {"id": item[0],
                 "content": item[1],
                 "url": item[2],
                 "sourceType": item[3]}
            list.append(d)
    return (list,count)


dict = {"sourceType": "XLWB", "content": "张", "page": "1", "limit": "10"}
data = getDataByDataTypeAndContentlike(**dict)
print(data)
